package kk.controller;


import com.github.pagehelper.PageInfo;
import kk.domain.Member;
import kk.service.MemberService;
import kk.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private MemberService memberService;

    @Autowired
    private OrdersService ordersService;

    @RequestMapping("/findAll")
    @PreAuthorize("hasAnyRole('ROLE_USER','ROLE_ADMIN')")
    public ModelAndView findAll(Integer pageNum,Integer pageSize) throws Exception {
        List<Member> memberList = memberService.findAll(pageNum, pageSize);
        PageInfo pageInfo=new PageInfo(memberList);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("pageInfo",pageInfo);
        modelAndView.setViewName("member-list");
        return modelAndView;
    }

    @RequestMapping("/save")
    @PreAuthorize("hasAnyRole('ROLE_USER','ROLE_ADMIN')")
    public String save(Member member) throws Exception{
        memberService.save(member);
        return "redirect:/member/findAll?pageNum=1&pageSize=4";
    }

    @RequestMapping("/findById")
    @PreAuthorize("hasAnyRole('ROLE_USER','ROLE_ADMIN')")
    public ModelAndView findById(Integer id) throws Exception {
        ModelAndView modelAndView=new ModelAndView();
        Member member = memberService.findById(id);
        modelAndView.addObject("member",member);
        modelAndView.setViewName("member-edit");
        return modelAndView;
    }

    @RequestMapping("/update")
    @PreAuthorize("hasAnyRole('ROLE_USER','ROLE_ADMIN')")
    public String update(Member member) throws Exception {
        memberService.update(member);
        return "redirect:/member/findAll?pageNum=1&pageSize=4";
    }

    @RequestMapping("/deleteById")
    @PreAuthorize("hasAnyRole('ROLE_USER','ROLE_ADMIN')")
    public String deleteById(Integer id) throws Exception{
        //先根据会员的id在订单表中删除该会员下过的订单
        ordersService.deleteByMemberId(id);
        //然后再在会员表中删除会员信息
        memberService.deleteById(id);
        return "redirect:/member/findAll?pageNum=1&pageSize=4";
    }



    @RequestMapping("/deleteByIds")
    @PreAuthorize("hasAnyRole('ROLE_USER','ROLE_ADMIN')")
    public String deleteByIds(int[] ids) throws Exception{
        for (int id : ids) {
            //先根据会员的id在订单表中删除该会员下过的订单
            ordersService.deleteByMemberId(id);
            //然后再在会员表中删除会员信息
            memberService.deleteById(id);
        }
        return "redirect:/member/findAll?pageNum=1&pageSize=4";
    }


    @RequestMapping("/findAllForIntroduce")
    @PreAuthorize("hasAnyRole('ROLE_USER','ROLE_ADMIN')")
    public @ResponseBody List<Member> findAllForIntroduce()throws Exception{
        List<Member> memberList = memberService.findAllForIntroduce();
        return memberList;
    }

    @RequestMapping("/findLevelById")
    @PreAuthorize("hasAnyRole('ROLE_USER','ROLE_ADMIN')")
    public @ResponseBody String findLevelById(Integer id) throws Exception{
        String level = memberService.findLevelById(id);
        return level;
    }

    @RequestMapping("/findByName")
    @PreAuthorize("hasAnyRole('ROLE_USER','ROLE_ADMIN')")
    public ModelAndView findByName(String name,Integer pageNum,Integer pageSize) throws Exception {
        String newName = "%"+name+"%";
        List<Member> memberList = memberService.findByName(newName, pageNum, pageSize);
        PageInfo pageInfo=new PageInfo(memberList);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("pageInfo",pageInfo);
        modelAndView.addObject("name",name);
        modelAndView.setViewName("member-list");
        return modelAndView;
    }
}
